import {Navigate, useLocation} from 'react-router-dom'
import {useSelector} from 'react-redux'
import {RootState} from '../store/inedx.ts'
import {JSX} from "react";

interface Props {
    children: JSX.Element
}

/**
 * 受保护的路由（路由守护）
 */
function ProtectedRoute({children}: Props) {
    const {username, token} = useSelector((state: RootState) => state.auth)
    const location = useLocation()

    if (!username || !token) {
        return <Navigate to="/login" state={{from: location}} replace/>
    }

    return children
}

export default ProtectedRoute